/**@file
   Logger class header file.
   
   @par license 
   Copyright 2012 Lu, Ken. All rights reserved.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 
   @author: Lu, Ken (bluewish.ken.lu@gmail.com)
**/

#ifndef _USER_LOGGER_H_
#define _USER_LOGGER_H_

#include <stdint.h>

namespace ken {

#define u_print(fmt, ...) Logger::LogMessage(Logger::LOG_PRINT, __FUNCTION__, __LINE__, __FILE__, true, fmt, __VA_ARGS__)
#define u_debug(fmt, ...) Logger::LogMessage(Logger::LOG_DEBUG, __FUNCTION__, __LINE__, __FILE__, false, fmt, __VA_ARGS__)
#define u_info(fmt, ...)  Logger::LogMessage(Logger::LOG_INFO, __FUNCTION__, __LINE__, __FILE__, false, fmt, __VA_ARGS__)
#define u_warn(fmt, ...)  Logger::LogMessage(Logger::LOG_WARNING, __FUNCTION__, __LINE__, __FILE__, false, fmt, __VA_ARGS__)
#define u_error(fmt, ...) Logger::LogMessage(Logger::LOG_ERROR, __FUNCTION__, __LINE__, __FILE__, false, fmt, __VA_ARGS__)

#define u_assert(exp)                           \
        do {                                    \
            if (!(exp)) {                       \
                u_error("assert " #exp          \
                      " failed in file %s(%d)", \
                      __FILE__, __LINE__);      \
                DebugBreak();                \
            }                                   \
        } while (0)

#define u_func_enter() u_debug("%s: >>>", __FUNCTION__)
#define u_func_leave() u_debug("%s: <<<", __FUNCTION__)

#ifdef __cplusplus
#define u_throw(fmt, ...)                       \
    do {                                        \
        char err[512] = {0};                    \
                                                \
        Logger::LogMessage(Logger::LOG_ERROR,   \
            __FUNCTION__, __LINE__, __FILE__,   \
                false, fmt, __VA_ARGS__);       \
        sprintf_s(err, 512, fmt, __VA_ARGS__);  \
        throw runtime_error(err);               \
    } while (0)
#endif

class Logger
{
public:
    enum LEVEL {
        LOG_DEBUG,
        LOG_INFO,
        LOG_WARNING,
        LOG_ERROR,
        LOG_CRITICAL,
        LOG_PRINT,
    };

    static void SetLevel(LEVEL level);
    static void LogMessage(LEVEL level, const char* function, int line, const char* file, bool noprefix, const char* format, ...);

public:
    Logger(void);
    ~Logger(void);

private:
    static int32_t level_;
    static bool is_show_detail_;
};

}

#endif // _USER_LOGGER_H_